
#import "PUFoodsModel.h"

@implementation PUFoodsModels

+ (instancetype)shared
{
    static PUFoodsModels *shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[self alloc] init];
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"PUFoodData" ofType:@"json"];
        NSData *jsonData = [NSData dataWithContentsOfFile:path];
        NSMutableArray *data=[shared loaddata];
        NSMutableArray *arrayData = [NSMutableArray array];
        NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
        for (NSDictionary *dict in jsonArray) {
            PUFoodsModel *model = [PUFoodsModel mj_objectWithKeyValues:dict];
            for (PUFoodModel *food in model.foods) {
                food.count = 1 + arc4random_uniform(100);
                for (NSDictionary *dic in data) {
                    if ([food.name isEqualToString:dic[@"name"]]) {
                        food.isCollect=YES;
                    }
                }
            }
            [arrayData addObject:model];
        }
        shared.list = arrayData;
    });
    
    return shared;
}

-(NSMutableArray *)loaddata{
    //创建一个归档文件夹
    NSArray *library = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *strLib = [[library objectAtIndex:0] stringByAppendingPathComponent:@"archive"];
    BOOL directory = YES;
    if (![[NSFileManager defaultManager] fileExistsAtPath:strLib isDirectory:&directory])
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:strLib withIntermediateDirectories:NO attributes:nil error:nil];
    }
    //归档
    NSString *entityArchive = [strLib stringByAppendingPathComponent:[NSString stringWithFormat:@"collectDataArchive%@",[Config getID]]];
//    [NSKeyedArchiver archiveRootObject:data toFile:entityArchive];
    //数据源从归档中读出
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:entityArchive];
    NSLog(@"%@",array);
    if(array.count==0){
        array=[NSMutableArray array];
    }
    return array;
}

@end

@implementation PUFoodsModel

+ (NSDictionary *)mj_objectClassInArray{
    return @{@"foods": [PUFoodModel class]};
}

@end

@implementation PUFoodModel

+ (NSDictionary *)mj_objectClassInArray{
    return @{@"steps": [CPStepModel class]};
}

@end

@implementation CPStepModel

@end
